package admin

import (
	"booking/common/handler"
	"booking/common/validator"
	"booking/models"

	"github.com/gin-gonic/gin"
)

func Del(c *gin.Context) {
	var params struct {
		Id uint `json:"id" binding:"required,gt=0"`
	}
	if err := c.ShouldBindJSON(&params); err != nil {
		handler.Error(validator.Translate(err), c)
		return
	}

	var admin models.Admin
	models.Db.Model(models.Admin{}).
		Where("id = ?", params.Id).First(&admin)
	if admin.Id <= 0 {
		handler.Error("用户不存在", c)
		return
	}
	if admin.Id == 1 {
		handler.Error("您无删除此用户权限", c)
		return
	}
	err := models.Db.Model(models.Admin{}).Delete(&admin).Error
	if err != nil {
		handler.Error(err.Error(), c)
		return
	}
	handler.Success(nil, c)
}
